var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    currentTab1: 0,
    page: 1,
    showModaling: false,
   
    currentId: 1,
    currentingId: 0,
    // x轴方向的偏移
    x: 0,
    // 当前x的值
    currentX: 0,
    y: 0,
    // 当前x的值
    currentY: 0,
   
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },

  //左滑动删除
  handleMovableChange: function (e) {
    // this.data.currentX = e.detail.x;
    this.data.currentX = e.detail.x;
  },

  handleTouchend: function (e) {
    this.isMove = true;
    if (this.data.currentX < -46) {
      this.data.x = -92;
      this.setData({
        x: this.data.x
      });
    } else {
      this.data.x = 0;
      this.setData({
        x: this.data.x
      });
    }
  },
  submiting: function (e) {
    var delmsgid = e.currentTarget.dataset.tagid
    this.setData({
      delmsgid: delmsgid,
      showModaling: true,
    })
  },
  submitingmesgno: function (e) {
    this.setData({
      showModaling: false,
    })
  },
  delMsgid: function (e) {
    var that = this
    var delmsgid = that.data.delmsgid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/sysmessage/del'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["u"] = u
      k["exhibitionid"] = exhibitionid
      k["msgid"] = delmsgid
      var sign = app.globalData.picotoken + 'clientios' + 'exhibitionid' + exhibitionid + 'msgid' + delmsgid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          that.setData({
            showModaling: false,
          })
          var keyword = that.data.keyword
          //获取缓存展会id和用户id存在则获取最新联系人列表
          var exhibitionid = wx.getStorageSync('exhibitionid')
          //  exhibitionid='33'
          var u = wx.getStorageSync('u')
          console.log('exhibitionid=' + exhibitionid)
          console.log('keyword=' + keyword)
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            //默认最新（调用pico接口）
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '/attendee/sysmessage/list'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["u"] = u
            var sign = ''
            if (!keyword || keyword == '') {
              sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            } else {
              k["keyword"] = keyword
              sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'keyword' + keyword + 'timestamp' + timestamp + app.globalData.picotoken
            }
            console.log(sign)
            var curb64 = md5.zhMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  noticeMesg: curdata
                });
              }, fail: res => {
                console.log(res, "error")
              }

            })

          }
        }, fail: res => {
          console.log(res, "error")
        }
      })
    }
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  preventTouchMove: function () {

  },
  toDetail: function (e) {
    var that = this;
    var tagid = e.currentTarget.dataset.tagid;
    console.log('tagid', tagid)
    wx.navigateTo({
      url: '../messageModule/notificationDetail?msgid=' + tagid,
    })
  },
  allisread: function () {
    var that = this
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //  exhibitionid='33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/sysmessage/allread'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            //默认最新（调用pico接口）
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '/attendee/sysmessage/list'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["u"] = u

            var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.hexMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  noticeMesg: curdata
                });
                wx.showToast({
                  title: '已全部已读',
                  icon: 'success',
                  duration: 2000
                })  
              }, fail: res => {
                console.log(res, "error")
              }
            })
          } 
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  go: function (e) {
    var that = this;
    //获取关键词
    var keyword = e.detail.value
    
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //  exhibitionid='33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('keyword=' + keyword)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/sysmessage/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      var sign = ''
      if (!keyword || keyword == '') {
        that.setData({
          keyword: ''
        });
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      } else {
        that.setData({
          keyword: keyword
        });
        k["keyword"] = keyword
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'keyword' + keyword + 'timestamp' + timestamp + app.globalData.picotoken
      }
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            noticeMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onShow: function (options) {
    var that = this;
    //获取关键词
    var keyword = that.data.keyword

    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //  exhibitionid='33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('keyword=' + keyword)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/sysmessage/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      var sign = ''
      if (!keyword || keyword == '') {
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      } else {
        k["keyword"] = keyword
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'keyword' + keyword + 'timestamp' + timestamp + app.globalData.picotoken
      }
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            noticeMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Notification list'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '通知列表'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
  //  exhibitionid='33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/sysmessage/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            noticeMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
})